package com.easy;

/**
 * Created by Lxk on 2020/1/23.
 */
public class Solution110 {

    private int depth(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int left = depth(node.left);
        if (Integer.valueOf(left).equals(-1)) {
            return -1;
        }
        int right = depth(node.right);
        if (Integer.valueOf(right).equals(-1)) {
            return -1;
        }
        if (Math.abs(left - right) < 2) {
            return Math.max(left, right) + 1;
        } else {
            return -1;
        }
    }

    public boolean isBalanced(TreeNode root) {
        return depth(root) != -1;
    }

}
